package cn.itcast.bos.service.base.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.base.CourierRepository;
import cn.itcast.bos.dao.base.FixedAreaRepository;
import cn.itcast.bos.dao.base.TakeTimeRepository;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.TakeTime;
import cn.itcast.bos.service.base.FixedAreaService;
@Service
@Transactional
public class FixedAreaServiceImpl implements FixedAreaService {
    @Autowired
    private FixedAreaRepository fixedAreaRepository;
	@Override
	public void saveFixedArea(FixedArea model) {
		// TODO Auto-generated method stub
		fixedAreaRepository.save(model);
	}
	@Override
	public Page<FixedArea> findByParams(Specification<FixedArea> specification, Pageable pageable) {
		// TODO Auto-generated method stub
		return fixedAreaRepository.findAll(specification,pageable);
	}
	//查找快递员
	@Autowired
	private  CourierRepository courierRepository;
	//查找排版时间
	@Autowired
	private TakeTimeRepository takeTimeRepository;
	@Override
	public void associationCourierToFixedArea(FixedArea fixedArea, Integer courierId, Integer takeTimeId) {
		//根据id查找定区
		FixedArea presistFindArea = fixedAreaRepository.findOne(fixedArea.getId());
		//根据快递员id查找快递员
		Courier courier = courierRepository.findOne(courierId);
		//根据排班时间id查找排班
		TakeTime takeTime = takeTimeRepository.findOne(takeTimeId);
		//将快递员关联到定区(多表关联，外键由定区维护）
		presistFindArea.getCouriers().add(courier);
		//将排班时间关联到快递员
		courier.setTakeTime(takeTime);
		
	}

}
